package com.ai.chat; /**
 * @author: heJinQin
 * @create: 2024-02-21 21:46
 * @description:
 **/



import io.github.asleepyfish.service.OpenAiProxyService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class HttpT {

    private static final String OPENAI_URL = "https://api.openai.com/v1/chat/completions";
    private static final String API_KEY = "sk-";
    private static final OkHttpClient httpClient = new OkHttpClient();

    public static void main(String[] args) {


        String prompt = "生成一个关于'美女'的创造性描述：";
        try {
            String response = generatePrompt(prompt);
            System.out.println("Generated Prompt: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String generatePrompt(String inputText) throws IOException {
        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        String jsonBody = "{"
                + "\"model\": \"gpt-3.5-turbo\","
                + "\"prompt\": \"" + inputText + "\","
                + "\"temperature\": 0.7,"
                + "\"max_tokens\": 100"
                + "}";

        RequestBody body = RequestBody.create(jsonBody, JSON);
        Request request = new Request.Builder()
                .url(OPENAI_URL)
                .addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + API_KEY)
                .post(body)
                .build();

        try (Response response = httpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            return response.body().string();
        }
    }




}
